package com.ruoyi.project.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.project.system.domain.SysAttribute;
import com.ruoyi.project.system.domain.SysType;
import com.ruoyi.project.system.service.ISysAttributeService;
import com.ruoyi.project.system.service.ISysTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysTypeDetail;
import com.ruoyi.project.system.service.ISysTypeDetailService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 分类详情Controller
 *
 * @author jjp
 * @date 2020-04-16
 */
@RestController
@RequestMapping("/system/detail")
public class SysTypeDetailController extends BaseController {
    @Autowired
    private ISysTypeDetailService sysTypeDetailService;
    @Autowired
    private ISysTypeService sysTypeService;
    @Autowired
    private ISysAttributeService sysAttributeService;


    @PreAuthorize("@ss.hasPermi('system:type:GeiList')")
    @GetMapping("/GetList")
    public AjaxResult list(SysType sysType) {
//        startPage();
        List<SysType> types = sysTypeService.selectSysTypeList(sysType);
        return AjaxResult.success(sysTypeService.buildTypeTree(types));
    }

    /**
     * 查询分类详情列表
     */
    @PreAuthorize("@ss.hasPermi('system:detail:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTypeDetail sysTypeDetail) {
        startPage();
        return getDataTable(sysTypeDetailService.selectSysTypeDetailList(sysTypeDetail));
    }

    /**
     * 获取分类详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:detail:query')")
    @GetMapping(value = "/{typeDetailId}")
    public AjaxResult getInfo(@PathVariable("typeDetailId") Long typeDetailId) {

        return AjaxResult.success(sysTypeDetailService.selectSysTypeIdById(typeDetailId));
    }

    /**
     * 新增分类详情
     */
    @PreAuthorize("@ss.hasPermi('system:detail:')")
    @Log(title = "分类详情", businessType = BusinessType.INSERT)
    @PostMapping("/addDetail")
    public AjaxResult add(@RequestBody SysTypeDetail sysTypeDetail) {
        Long typeId = sysTypeDetail.getTypeId();
        SysType sysType = new SysType();
        sysTypeDetailService.insertSysTypeDetail(sysTypeDetail);
        Long typeDetailId = sysTypeDetail.getTypeDetailId();
        sysType.setTypeId(typeId);
        sysType.setTypeDetailId(typeDetailId);
        sysTypeService.updateSysTypes(sysType);
        Map<String, Object> map = new HashMap<>();
        map.put("typeId", typeId);
        map.put("typeDetailId", typeDetailId);
        return AjaxResult.success(map);
    }

    /**
     * 修改分类详情
     */
    @PreAuthorize("@ss.hasPermi('system:detail:edit')")
    @Log(title = "分类详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTypeDetail sysTypeDetail) {
        sysTypeDetailService.updateSysTypeDetail(sysTypeDetail);
        return AjaxResult.success();
    }

    /**
     * 删除分类详情
     */
    @PreAuthorize("@ss.hasPermi('system:detail:remove')")
    @Log(title = "分类详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeDetailIds}")
    public AjaxResult remove(@PathVariable Long[] typeDetailIds) {
        return toAjax(sysTypeDetailService.deleteSysTypeDetailByIds(typeDetailIds));
    }
}
